/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.totoland.web.utils;

import java.security.Key;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author totoland
 */
public class WebUtils {

    public static String generateToken() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static void main(String args[]) throws Exception {
        
        String passWord = "password";
        
        String nPassWord = WebUtils.encrypt(passWord);
        
        System.out.println("nPassWord : "+nPassWord);
        
        System.out.println(WebUtils.decrypt(nPassWord));
        
    }
    
    private static final String ALGO = "AES";
    private static final byte[] keyValue =
            new byte[]{'E', 't', 'c', 'S', 'e', 'c', 'K',
        'e', 'y', '1', '2', '3', '4', '5', '6', '7'};

    public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }
    
    public static String lineDisplayTraceLog(String logger){
        
        return "*********************************"+logger+"*********************************";
        
    }
}
